/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufba.lasid.mda.services.replicator;

import br.ufba.lasid.base.api.IResource;
import br.ufba.lasid.ds.fd.FailureDetector;
import br.ufba.lasid.jitclouds.mda.JiTContext;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author cloud15
 */
@WebService(serviceName = "JiTReplicatedResourceService")
public class JiTReplicatedResourceService {

    private static final Log logger = LogFactory.getLog(JiTReplicatedResourceService.class);

    /**
     * This is a sample web service operation
     */
    @WebMethod(operationName = "hello")
    public String hello(@WebParam(name = "name") String txt) {
        return "Hello " + txt + " !";
    }

    /**
     * Operação de Web service
     */
    @WebMethod(operationName = "register")
    public String register(@WebParam(name = "hostID") String hostID, @WebParam(name = "localhost") boolean localhost) {
        try {
            JiTContext.getReplicationManager().register(hostID, localhost);
        } catch (Exception ex) {
            logger.error("Couldn't register host " + hostID, ex);
            return Boolean.FALSE.toString();
        }
        return Boolean.TRUE.toString();
    }

    /**
     * Operação de Web service
     */
    @WebMethod(operationName = "setPeriod")
    public String setPeriod(@WebParam(name = "period") long period) {
        try {
            JiTContext.getReplicationManager().setPeriod(period);
        } catch (Exception ex) {
            logger.error("Couldn't set period " + period, ex);
            return Boolean.FALSE.toString();
        }
        return Boolean.TRUE.toString();
    }

    /**
     * Operação de Web service
     */
    @WebMethod(operationName = "query")
    public String query(@WebParam(name = "resourceAddress") String resourceAddress) {
        try {
            IResource.States state = JiTContext.getReplicationManager().query(resourceAddress);
            return state.toString();
        } catch (Exception ex) {
            logger.error("Couldn't query " + resourceAddress, ex);
            return Boolean.FALSE.toString();
        }
        // return Boolean.TRUE.toString();
    }

    /**
     * Operação de Web service
     */
    @WebMethod(operationName = "buildup")
    public String buildup() {
        try {
            
            FailureDetector detector = new FailureDetector();
            detector.register("192.168.188.15", true);
            detector.register("192.168.188.16", false);
            
            JiTContext.getReplicationManager().setDetector(detector);
            JiTContext.getReplicationManager().getDetector().buildup();
            JiTContext.getReplicationManager().getDetector().setPeriod(5000L);
            JiTContext.getReplicationManager().getDetector().setTimeout(200L);
            JiTContext.getReplicationManager().getDetector().startup();

            JiTContext.getReplicationManager().buildup();
        } catch (Exception ex) {
            logger.error("Couldn't build up.", ex);
            return Boolean.FALSE.toString();
        }
        return Boolean.TRUE.toString();
    }

    /**
     * Operação de Web service
     */
    @WebMethod(operationName = "startup")
    public String startup() {
        try {
            JiTContext.getReplicationManager().startup();
        } catch (Exception ex) {
            logger.error("Couldn't start up.", ex);
            return Boolean.FALSE.toString();
        }
        return Boolean.TRUE.toString();
    }
}
